<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .nav{
            width: 1200px;
            height: 700px;
            background-color: #ffffff;
            margin:30px auto;
            box-shadow: 5px 5px 10px;
            border-radius: 10px;
            background-color: #cec5c5;
        }
        .left{
            width: 65%;
            height: 100%;
            float: left;
            background-color: #fff;
            border-radius: 10px;
        }
        .da{
            width: 20%;
            height: 37%;
            background-color: #f3f3f3;
            text-align: center;
            line-height: 25px;
            float: left;
            margin: 20px 10px;
            
        }
        .da img{
        width: 100px;
        height: 150px;
        margin-top: 8px;
        
     }   
     .box{
        width: 96%;
        height: 34%;
        margin: auto;
        background-color: #ffffff;
        border-radius: 10px;
     }
     .box div{
        width: 60%;
        height: 35%;
        background-color: red;
        margin: auto;
     
     }
     .box p{
        margin: 5px;
        height: 16px;
     }
     .right{
        width: 34%;
        height: 100%;
        float: right;
        background-color: #fff;
        border-radius: 10px;
     }
     .tou{
        display: flex;
        margin:10px 30px;
        justify-content:space-between;
     }
     li{
        list-style: none;
       
        margin-top: 10px;
        display: flex;
        
     }
     li div{
        margin-left: 20px;
     }
     .in{
        margin: 50px 10px 0 -20px;
     }
    </style>
</head>

<body>
    <div class="nav">
        <div class="left">
            <!-- <div class="da">
                <img src="" alt="">
                <div class="box">
                    <p>时装</p>
                    <span class="dan">¥100</span><br>
                    <div>加入购物车</div>
                </div>
            </div> -->
        </div>
        <div class="right">
            <div class="tou">
                <div><b>购物车</b></div>
                <div>总计：<span class="zongji">0</span></div>
                <div>总价：<span class="zongjia">0</span></div>
                <div>全选
                    <input type="checkbox" class="quan">
                </div>
            </div>
            <ul>
                <!-- <li>
                    <input type="checkbox" class="in">
                    <img src="./img/1.jpg_Q75.jpg" alt="" width="80px">
                    <div>
                        <p>${item.title}</p>
                    <p>单价：<span class="dan">${item.money}</span></p>
                    <button class="aa">-</button>
                    <input type="text" value="${item.num}"size="3" class="put">
                    <button class="bb">+</button>
                    <span class="an">小计:${item.}</span>
                <span class="sp">删除</span>
                    </div>
                </li> -->
            </ul>
        </div>
        
    </div>
</body>

</html>
<script>
    let data=[
            {id: "1001",
            imgurl: "./img/1.jpg_Q75.jpg",
            title: "时装0",
            money: 100,
            num: "1",
        },
        {id: "1002",
            imgurl: "img/1.webp",
            title: "时装1",
            money: 100,
            num: "1",
        },
        {id: "1003",
            imgurl: "img/2.jpg_.webp",
            title: "时装2",
            money: 100,
            num: "1",
        },
        {id: "1004",
            imgurl: "img/3.webp",
            title: "时装3",
            money: 100,
            num: "1",
        },
        {id: "1005",
            imgurl: "img/4.webp",
            title: "时装4",
            money: 100,
            num: "1",
        },
        {id: "1006",
            imgurl: "img/5.webp",
            title: "时装5",
            money: 100,
            num: "1",
        },
        {id: "1007",
            imgurl: "img/6.webp",
            title: "时装6",
            money: 100,
            num: "1",
        },
        ]
      let drae=[]
       function render(){
        let oLeft=document.querySelector(".left") 
        data.forEach(function(item,index){
            oLeft.innerHTML+=
            `<div class="da">
            <img src="${item.imgurl}" alt="">
            <div class="box">
            <p>${item.title}</p>
            <span class="dan">¥${item.money}</span><br>
            <div class="dian">加入购物车</div>
            </div>
            </div>`
        })
       }
       render()
       function gouwuche(){
        let oUl=document.querySelector("ul")
        
            oUl.innerHTML=drae.map(function(item,index){
                return    `<li>
                    <input type="checkbox" class="in">
                    <img src="${item.imgurl}" alt="" width="80px">
                    <div>
                        <p>${item.title}</p>
                    <p>单价：<span class="dan">${item.money}</span></p>
                    <button class="aa">-</button>
                    <input type="text" value="${item.num}"size="3" class="put">
                    <button class="bb">+</button>
                    <span class="an">${item.money*item.num}</span>
                <span class="sp">删除</span>
                    </div>
                </li>`
       }).join('')
      
    }
    let oLeft=document.querySelector(".left") 
    oLeft.onclick=function(e){
            if(e.target.className=='dian'){
                var btns = [...document.getElementsByClassName('dian')]
                var index = btns.findIndex((item)=>{
                    return item == e.target;
                })
                if(drae.indexOf(data[index])===-1){
                    drae.push(data[index])
                    gouwuche()
                }else{
                    drae[index].num=Number(drae[index].num)+1
                    gouwuche()
                }    
                console.log(drae);
            }
            jiSuan()
        }
      let  oUl=document.querySelector("ul")
      oUl.onclick=function(e){
        if(e.target.className=='aa'){
                var btns = [...document.getElementsByClassName('aa')]
                var index = btns.findIndex((item)=>{
                    return item == e.target;
                })
                drae[index].num=Number(drae[index].num)-1
                if(drae[index].num<0){
                    drae[index].num=0
                }
                gouwuche()
            }
            if(e.target.className=='bb'){
                var btns = [...document.getElementsByClassName('bb')]
                var index = btns.findIndex((item)=>{
                    return item == e.target;
                })
                drae[index].num=Number(drae[index].num)+1
                gouwuche()
            }
            if(e.target.className=='sp'){
                var oSp = [...document.getElementsByClassName('sp')]
                var index = oSp.findIndex((item)=>{
                    return item == e.target;
            })
            
               
            drae[index].num="1";
               drae.splice(drae.indexOf(index), 1)
               
               gouwuche()
            }
            jiSuan()
      }
      function jiSuan(){
    var zong=document.querySelector(".quan")
    var zongJi=document.querySelector(".zongji")
    var zongJia=document.querySelector(".zongjia")
	var inputIn = document.querySelectorAll(".in");
        zong.addEventListener('click', function () {
            let sum=0;
            let snm=0;
            for (let i = 0; i < inputIn.length; i++) {
                inputIn[i].checked = this.checked;
                sum+=Number(drae[i].num)
                snm+=parseFloat(oUl.querySelector('.an').innerHTML)
            }
            zongJi.innerHTML=sum
            zongJia.innerHTML=snm;
            if(this.checked==false){
                    zongJi.innerHTML=0;
                    zongJia.innerHTML=0;
                }
        });
        for (let i = 0; i < inputIn.length; i++) {
            inputIn[i].addEventListener('click', function () {
                var   flag = true;
                let sum=0;
                let snm=0;
                for (let j = 0; j < inputIn.length; j++) {
                    if (inputIn[j].checked == false) {
                        flag = false;
                    }
                    if(inputIn[j].checked){
                    sum+=Number(drae[j].num)
                    snm+=Number(drae[j].money)
                }
                
                }
                zong.checked = flag;
                zongJi.innerHTML=sum
                zongJia.innerHTML=snm;
            });
            
        }
      }
     
</script>
